package com.burgeon.backend.SHOP.service.Impl;

import com.burgeon.backend.SHOP.entity.Sign;
import com.burgeon.backend.SHOP.entity.SignConfig;
import com.burgeon.backend.SHOP.mappers.AppUserMapper;
import com.burgeon.backend.SHOP.mappers.SignMapper;
import com.burgeon.backend.SHOP.rest.CreateIntegral;
import com.burgeon.backend.SHOP.rest.CreateIntegralByAward;
import com.burgeon.backend.SHOP.service.SignService;
import com.burgeon.backend.SHOP.service.ex.InsertExceptionApp;
import com.burgeon.backend.entity.Info;
import com.burgeon.backend.entity.User;
import com.burgeon.backend.mappers.AccountMapper;
import com.burgeon.backend.mappers.UserMapper;
import com.burgeon.backend.service.ex.InsertException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.xml.ws.Action;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;

@Service
public class SignServiceImpl implements SignService {
    @Autowired
    private SignMapper signMapper;
    @Autowired
    private AppUserMapper userMapper;
    @Autowired
    private AccountMapper accountMapper;


    @Override
    public Integer insertSign(String openid, String uid, String token,String phone) throws InsertExceptionApp{

        Sign sign =signMapper.findSignLast(uid,token);

        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String signTime = sdf.format(date);     //当前的时间

        if (sign!=null ) { //没有纪录，第一次签到
            String oldTime = sign.getSigntime();//前一次的纪录
            Date old = null;  //转换为时间格式
            try {
                old = sdf.parse(oldTime);
            } catch (ParseException e) {
                System.err.println("签到");
                e.printStackTrace();
            }
            Calendar cmCal = Calendar.getInstance();//比较时间
            Calendar bmCal = Calendar.getInstance();
            cmCal.setTime(old);
            bmCal.setTime(date);
            //bmCal.getTime();
            boolean isSameYear = cmCal.get(Calendar.YEAR) == bmCal
                    .get(Calendar.YEAR);
            boolean isSameMonth = isSameYear
                    && cmCal.get(Calendar.MONTH) == bmCal.get(Calendar.MONTH);
            boolean isSameDate = isSameMonth
                    && cmCal.get(Calendar.DAY_OF_MONTH) == bmCal
                    .get(Calendar.DAY_OF_MONTH);
            if (isSameDate){
                throw new InsertExceptionApp("已签到");//同一天不能签到
            }
            if (!isSameMonth) {
                signMapper.deleteSign(uid);//删除上个月所有纪录
            }
        }
        return insertSi(openid, uid, token, signTime,phone);
        }

    private Integer insertSi(String openid, String uid, String token, String signTime,String phone) {
        System.err.println(openid+"."+token);
        User user1 = userMapper.GetUserByOpenid(openid, token);
        System.err.println(user1);
        Sign sign1 = new Sign();
        sign1.setUid(uid);
        sign1.setUname(user1.getName());
        sign1.setPhone(phone);
        sign1.setSigntime(signTime);
        sign1.setToken(token);
        int r = signMapper.insertSign(sign1);

        SignConfig signConfig = signMapper.findSignCfg(token);//根据token获取签到配置的积分增加数
        String integral =signConfig.getIntegral();
        System.err.println(integral);
        if(Integer.parseInt(integral)>0){
            Info info = GetInfo(token);
            CreateIntegralByAward g = new CreateIntegralByAward(info.getUrl(), info.getAccount(), info.getPassword());
            try {
                g.getData(user1.getPhone(), integral);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return r ;
    }

    @Override
    public Integer insertSignCfg(SignConfig signConfig) {
        return signMapper.insertSignCfg(signConfig);
    }

    @Override
    public HashMap findSignToday(String uid,String token) {
        //Sign sign =signMapper.findSignLast(uid,token);
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String signTime = sdf.format(date);
        HashMap data = new HashMap();

        SignConfig signConfig = signMapper.findSignCfg(token);
        data.put("integral",signConfig.getIntegral());
        if (null == signMapper.findSignToday(uid,token,signTime)){
            data.put("use","N");
        }else {
            data.put("use","Y");
        }

        return data;
    }
    //private boolean isSame() {}

    private Info GetInfo(String token) {
        return accountMapper.GetTokenData(token);
    }
}
